package com.itheima.demo01;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test01 {
    public static void main(String[] args) throws IOException {
        //1.创建姓氏、名字的url地址
        String firstNameNet = "https://xm.liemingwang.com/baijiaxing.html";
        //男生名字
        String boyNameNet = "http://mingzi.jb51.net/qiming/nanhai/78895437111.html";
        //女生名字
        String girlNameNet = "http://mingzi.jb51.net/qiming/nvhai/68815554142.html";

        //调用方法获取姓氏数据
        String firstNameStr = getData(firstNameNet);
        String boyNameStr = getData(boyNameNet);
        String girlNameStr = getData(girlNameNet);

        //通过正则获取姓氏数据
        ArrayList<String> firstNameList = getMatchingData(firstNameStr, "\">(.{1})</a>", 1);
        //通过正则获取男生名字数据
        ArrayList<String> boyNameList = getMatchingData(boyNameStr, "([\\u4e00-\\u9fa5]{2})(、|</p>)", 1);
        //通过正则获取女生名字数据
        ArrayList<String> girlNameTempList = getMatchingData(girlNameStr, "((\\d+)\\.)([\\u4e00-\\u9fa5、]+)", 3);

        //处理女生名字
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String name : girlNameTempList) {
            String[] nms = name.split("、");
            girlNameList.addAll(Arrays.asList(nms));
        }

        ArrayList<String> userInfoList = getUserInfo(firstNameList, boyNameList, girlNameList, 60, 70);
        Collections.shuffle(userInfoList);
        //遍历用户信息 保存文username.txt文件中
        File file = new File("username.txt");
        BufferedWriter nf = new BufferedWriter(new FileWriter(file));
        for (String name : userInfoList) {
            nf.write(name);
            nf.newLine();
        }
        nf.close();

    }

    //获取用户信息
    public static ArrayList<String> getUserInfo(ArrayList<String> firstNameList, ArrayList<String> boyNameList, ArrayList<String> girlNameList, int boyCount, int girlCount) {
        ArrayList<String> list = new ArrayList<>();

        //打乱姓氏顺序
        Collections.shuffle(firstNameList);
        //打乱名字顺序
        Collections.shuffle(boyNameList);
        Collections.shuffle(girlNameList);

        Random random = new Random();
        //生成男生姓名
        for (int i = 0; i < boyCount; i++) {
            list.add(firstNameList.get(i) + boyNameList.get(i) + "-" + "男" + "-" + (random.nextInt(8) + 19));
        }
        //生成女生姓名
        for (int i = 0; i < girlCount; i++) {
            list.add(firstNameList.get(i) + girlNameList.get(i) + "-" + "女" + "-" + (random.nextInt(7) + 17));
        }
        return list;
    }

    public static ArrayList<String> getMatchingData(String str, String reg, int index) {
        ArrayList<String> list = new ArrayList<>();

        Pattern pattern = Pattern.compile(reg);

        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            String group = matcher.group(index);
            list.add(group);
        }

        return list;
    }

    /**
     * 方法的作用:
     * 通过网络地址获取数据
     *
     * @return
     */
    public static String getData(String net) throws IOException {
        //创建字符串集合
        StringBuilder sb = new StringBuilder();

        //创建url
        URL url = new URL(net);

        //打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = connection.getInputStream();
        //把字符流转为字节流
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String len;
        while ((len = reader.readLine()) != null) {
            sb.append(reader.readLine());
        }

        return sb.toString();
    }

}
